package marchsoft.config;

import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.Nullable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @title q
 * @description
 * @author Lenovo
 * @version 1.0
 * @create 2024-09-27 17:36
 */

public class StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {
    @Nullable
    @Override
    public LocalDateTime convert(String json) {
        System.out.println(json.length());
        System.out.println(json);
        if (json.length() == 10) {
            DateTimeFormatter  dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            return LocalDateTime.parse(json, dateFormatter);
        }
        if (json.length() == 13) {
            DateTimeFormatter  dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
            return LocalDateTime.parse(json, dateFormatter);
        }

        if (json.length() == 16) {
            DateTimeFormatter  dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            return LocalDateTime.parse(json, dateFormatter);

        }
        if (json.length() == 19) {
            DateTimeFormatter  dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            return LocalDateTime.parse(json, dateFormatter);
        }
        return null;
    }

}
